package com.hopedove.callsystem.activity.MaopiCheck

import android.os.Bundle
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.hopedove.base.BaseActivity
import com.hopedove.callsystem.R
import com.hopedove.callsystem.databinding.ActivityCallProductionLayoutBinding
import com.hopedove.callsystem.domain.net.GetProductionFullCar
import com.hopedove.callsystem.domain.net.ProductionCall
import com.hopedove.net.ErrResponse
import com.hopedove.net.OkResponse
import com.hopedove.net.domain.Communication
import com.hopedove.utils.ToastUtil
import com.hopedove.view.SimpleDialog
import com.hopedove.view.TipsDialogFragment
import com.hoperun.callsystem.adapter.CarAdapter
import com.hoperun.callsystem.entity.CarEntity
import org.json.JSONObject

class SCQCallActivity : BaseActivity() {

    private lateinit var mAdapter: CarAdapter
    var mClothTktNo: String = ""

    lateinit var binding: ActivityCallProductionLayoutBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCallProductionLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initViewAndEvent()
        initData()
    }


    private fun initViewAndEvent() {
        mClothTktNo = intent?.getStringExtra("clothtktNo").toString()
        mAdapter = CarAdapter(1)
        mAdapter.setOnItemClickListener { adapter, view, position ->
            mAdapter.setCurrent(position)
        }
        binding.recyclerview.adapter = mAdapter
        binding.callBtn.setOnClickListener { startCall() }

    }

    private fun startCall() {
        if (mAdapter.selection == -1) {
            ToastUtil.longToast(this, "请选择后再呼叫！")
            return
        }
        showWait()
        var whlocoNo = (mAdapter.data[mAdapter.selection] as CarEntity).WHLOCNO
        Communication.asyncRequest(ProductionCall(whlocoNo, mClothTktNo)) { r ->
            stopWait()
            if (r is OkResponse) {
                val view = layoutInflater.inflate(R.layout.callsys_tips_type_2, null)
                val tipDialog = TipsDialogFragment.newInstance(view)
                tipDialog.isCancelable = false
                tipDialog.show(supportFragmentManager, "tipDialog")
                uiHandler.postDelayed({
                    tipDialog.dismiss()
                    this.finish()
                }, 3000)
            } else if (r is ErrResponse) {
                SimpleDialog.show(this, "生产区呼叫失败！")
            }
        }
    }


    private fun initData() {
        showWait()

        Communication.asyncRequest(GetProductionFullCar()) { r ->
            stopWait()
            if (r is OkResponse) {
                val jsonObj = JSONObject(r.data)
                val data = jsonObj.optJSONArray("DATA")
                val carList = Gson().fromJson<List<CarEntity>>(
                    data.toString(),
                    object : TypeToken<List<CarEntity>>() {}.type
                )
                mAdapter.setNewData(carList)
            } else if (r is ErrResponse) {
                SimpleDialog.show(this, "获取生产区满车区库位失败！")
            }

        }

    }

}